#include "../include/Configuration.h"

#include <fstream>
#include <iostream>
#include <sstream>

using std::ofstream;
using std::ifstream;
using std::istringstream;
using std::ostringstream;
using std::cout;
using std::endl;

Configuration::Configuration(const string &filepath) 
: _filepath(filepath)
{
    //create file
    ofstream ofs(filepath, std::ios::out|std::ios::app);
    if (!ofs.good()) {
        cout <<"ofstrem open file error" << endl;
        return;
    }
    ofs.close();

    //set stopWordList
    set<string> mySet = {"·", "×", "à", "á", "é","ì","í","ò","ù","ú","ü","ā","ē",
                    "ī","ū","ǐ","—","‘","’","“","”","…","″","Ⅰ","Ⅳ","Ⅵ","①",
                    "②","③","④","⑤","⑥","⑦","⑧","⑨","⑩","⑴","⑵","⑶","⑿","○",
                    "、","。","〈","〉","《","》","【","】","〔","〕","！",
                    "（","）","＊","，","－","．","／","０","1","2","3",
                    "4","5","6","7","8","9",":",";","<",">","?",
                    "＠","Ｇ","Ｊ","Ｌ","Ｍ","Ｓ","Ｔ","［","］","ｃ","ｄ",
                    "ｆ","ｇ","ｈ","ｉ","ｊ","ｎ","ｏ","ｔ","ｕ","ｗ","ｘ",
                    "ｙ","ｚ","～"};
    mySet.swap(_stopWordList);
    for (char i = 33; i < 127; ++i) {
        if (i == ' ') {
            continue;
        }
        ostringstream oss;
        oss << i;
        _stopWordList.insert(oss.str());
    }
}

Configuration::~Configuration() {

}

map<string, string> &Configuration::getConfigMap() {
    ifstream ifs(_filepath);
    if (!ifs.good()) {
        cout << "ifstream open error!" << endl;
        exit(0);
    }

    string line;
    while (getline(ifs, line)) {
        istringstream iss(line);
        string word;
        iss >> word >> _configMap[word];
    }
    ifs.close();
    return _configMap;
}

set<string> Configuration::getStopWordList() {
    return _stopWordList;
}
